﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;
using LeHouse.Server.WebApi.Common;

namespace LeHouse.Server.WebApi.Controllers
{
    public class DepartmentController : ApiController
    {
        /// <summary>
        /// 部门的下拉框获取
        /// </summary>
        /// <returns>符合要求的下拉框数据</returns>
        [Route("api/Department/GetHouseType")]
        public List<System_Department> GetSelectHouseType()
        {
            //部门的实例化
            System_DepartmentService dic = new System_DepartmentService();
            List<System_Department> droplist = dic.Select();
            return droplist;
        }

        /// <summary>
        /// 显示部门信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Department/Select")]
        public List<System_Department> Select([FromBody]string dname)
        {
            //部门的实例化
            System_DepartmentService dep = new System_DepartmentService();
            if(string.IsNullOrEmpty(dname))
            {
               var d1=dep.Select();
               return d1;
            }
            else
            {
                var d1 = dep.Select(m=>m.T_Dname.Contains(dname.Trim())).ToList();
                return d1;
            }
        }

        /// <summary>
        /// 分页显示部门信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Department/SelectPage")]
        public SelectPager<System_Department> SelectPage([FromBody]SelectPager<System_Department> obj)
        {
            SelectPager<System_Department> sp = new SelectPager<System_Department>();
            System_DepartmentService department = new System_DepartmentService();
            if (string.IsNullOrEmpty(obj.search))
            {
                long totalSize;
                sp.Result = department.SelectByPager(obj.PageIndex, obj.PageSize, out totalSize);
                sp.PageCount = Convert.ToInt32(Math.Ceiling((double)totalSize / obj.PageSize));
                sp.TotalSize = Convert.ToInt32(totalSize);
            }
            else
            {
                long totalSize;
                sp.Result = department.SelectByPager(p => p.T_Dname.Contains(obj.search), obj.PageIndex, obj.PageSize, out totalSize);
                sp.PageCount = Convert.ToInt32(Math.Ceiling((double)totalSize / obj.PageSize));
                sp.TotalSize = Convert.ToInt32(totalSize);
            }
            return sp;
        }


        /// <summary>
        /// 删除部门表信息
        /// </summary>
        /// <param name="id">获取要删除的部门表信息</param>
        /// <returns>false/true</returns>
        [HttpPost]
        [Route("api/Department/Delete")]
        public bool Delete([FromBody]int id)
        {
            //部门的实例化
            System_DepartmentService delete = new System_DepartmentService();

            if (delete.DeleteById(id) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 添加部门信息
        /// </summary>
        /// <param name="dep"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("api/Department/Insert")]
        public bool Insert([FromBody]System_Department dep)
        {
         //   dep ="1";//状态；1代表启用，0代表未启用。
            System_DepartmentService Indert = new System_DepartmentService();
            dep.T_Dstate = Convert.ToInt32(EnumState.StateOK);
            if (Indert.Insert(dep) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 根据编号id获取部门表的信息
        /// </summary>
        /// <param name="id">根据编号获取信息</param>
        /// <returns>treu/false</returns>
        [HttpPost]
        [Route("api/Department/UpdateByid")]
        public System_Department UpdateByid([FromBody]int id)
        {
            System_DepartmentService updateByid = new System_DepartmentService();
            var d1 = updateByid.SelectById(id);
            return d1;
        }

        /// <summary>
        /// 根据id修改部门状态
        /// </summary>
        /// <param name="dep">获取已修改的信息</param>
        /// <returns>true/false</returns>
        [HttpPost]
        [Route("api/Department/UpdateState")]
        public bool UpdateState(System_Department obj)
        {
            System_DepartmentService update = new System_DepartmentService();
            var d1 = update.SelectById(Convert.ToInt32(obj.T_DepartmentID));
            d1.T_Dstate = obj.T_Dstate;
            if (update.Update(d1) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="dep">获取已修改的信息</param>
        /// <returns>true/false</returns>
        [HttpPost]
        [Route("api/Department/Update")]
        public bool Update([FromBody]System_Department dep)
        {
            System_DepartmentService update = new System_DepartmentService();
            if (update.Update(dep) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
